﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ArcherSoldier : MoveUnit
{
    private GameObject arrow;// 箭
    private Transform arrowPos;// 箭生成位置
    float timer;

    public ArcherSoldier():base()
    {

    }

    void Awake()
    {
        obj = transform.gameObject;
        timer = 0;
    }

    void Start()
    {
        // 基本属性
        currentHP = 100;
        attackRange = 10;
        attackDamage = 10;

        // 技能
        lastAttackTime = Time.time;
        attackInterval = 1.0f;

        // 士兵状态管理初始化
        state = new SoldierStateManager(obj);
        state.InitStateSystem();
        state.Init();

        // 寻路初始化
        m_NavAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();

        // 弓箭
        arrow = Resources.Load("Prefabs/Arrow") as GameObject;
        arrowPos = gameObject.transform.Find("ShootPoint");

        // 血条初始化
        BloodInit();
        reTrBloodThis.localScale = Vector3.one / 2;//指定大小
    }

    void Update()
    {
        // 实时更新目标
        FindTarget();
        // 士兵状态转变监听
        state.StateSystem.CurrentState.ProcessTransition();
    }

    /// <summary>
    /// 攻击函数
    /// </summary>
    public override void DoAttack()
    {

        obj.transform.LookAt(target.transform);

        if (timer >= attackInterval)
        {
            GameObject clone;
            clone = Instantiate(arrow, arrowPos.position, arrowPos.rotation);
            clone.GetComponent<Rigidbody>().velocity = clone.transform.TransformDirection(Vector3.down * 25);
            target.GetComponent<Unit>().BeAttacked(attackDamage);
            GameObject.Destroy(clone, 0.4f);
            timer = 0;
        }
        timer += Time.deltaTime;
    }

}
